package com.lldpet.spring.cloud.demo.price.service.controller;

import com.lldpet.spring.cloud.demo.price.client.event.PriceUpdateEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.bus.BusProperties;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @desc：PriceController
 * @author: guanliang.xue
 * @date: 2021/5/12 13:29
 * @version:
 * @since:
 */
@RestController
@RequestMapping("price")
public class PriceController {
    private static final Logger logger = LoggerFactory.getLogger(PriceController.class);

    /**
     * spring cloud bus 的配置
     */
    @Resource
    private BusProperties busProperties;

    @Resource
    private ApplicationEventPublisher applicationEventPublisher;

    @GetMapping("update")
    public String update(@RequestParam(name = "destination") String destination){
        logger.info("创建订单 >>>>>>");
        // 1. 计算商品价格
        // 2. 锁定库存
        // 3. 库存锁定成功

        String originService = busProperties.getId();
        PriceUpdateEvent priceUpdateEvent = new PriceUpdateEvent("价格服务",originService,destination,"goodsId","userId");
        applicationEventPublisher.publishEvent(priceUpdateEvent);
        return "success";
    }
}
